<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    li {
      list-style: none;
    }

    html,
    body,
    #box {
      width: 100%;
      height: 100%;
    }

    #box {
      background-color: #eee;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

    .footer {
      width: 100%;
      height: 40px;
      background-color: skyblue;
      display: flex;
      justify-content: space-around;
      line-height: 40px;
    }

    .content {
      width: 100%;
      flex: 1;
      background-color: orange;
    }
  </style>
</head>

<body>
  <div id="box">
    <div class="content">
      <!-- 这是页面内容的容器 -->
    </div>
    <ul class="footer">
      <li path="/home">首页</li>
      <li path="/goodstype">商品分类</li>
      <li path="/car">购物车</li>
      <li path="/collection">收藏</li>
      <li path="/my">我的</li>
    </ul>
  </div>
  <script src="./js/home.js"></script>
  <script src="./js/goodstype.js"></script>
  <script src="./js/car.js"></script>
  <script src="./js/my.js"></script>
  <script src="./js/collection.js"></script>

  <script>

    const routes = [
      {
        path: "/home",
        component: home
      },
      {
        path: "/goodstype",
        component: goodstype
      },
      {
        path: "/car",
        component: car
      },
      {
        path: "/my",
        component: my
      },
      {
        path: "/collection",
        component: collection
      }
    ]


    window.onload = function () {
      initEvent();
    }

    window.onpopstate = function(){
      // 后退
      console.log("onpopstate");
      console.log("history.state", history.state);
      
      goPage(history.state.path,false);

    }

    function initEvent() {
        liClick();
    }

    function liClick(){
      let liDoms = document.querySelectorAll("li");
      
      for (let i = 0; i < liDoms.length; i++) {
        liDoms[i].onclick = function () {
            goPage(this.getAttribute("path"));
        }
      }
    }

    function goPage(path,isSaveHistory=true){
      let div = document.querySelector(".content");
      // 页面的替换（显示）
      div.innerHTML = routes.filter(route => route.path === path)[0].component;
      // 记录历史记录
      isSaveHistory && history.pushState({path},"");
    }

  </script>
</body>

</html>